#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "sys/ioctl.h"

/* 定义命令 */
#define CLOSE_CMD     (_IO(0XEF, 0X1))
#define OPEN_CMD      (_IO(0XEF, 0X2))
#define SETPERIOD_CMD (_IOW(0XEF, 0X3,int))



/*

*/
int main(int argc, char *argv[]){
	int fd, ret;
	unsigned int cmd;
	unsigned int arg;
	char *filename;
	unsigned char str[99];

	if (argc != 2) {
		printf("Error Usage!\r\n");
		return -1;
	}
	
	filename = argv[1];
	fd = open(filename, O_RDWR);
	if(fd < 0) {
		printf("file %s open failed\r\n",argv[1]);
		return -1;
	}

	while (1)
	{
		printf("input CMD: ");
		ret = scanf("%d", &cmd);
		if(ret != 1){
			gets(str);
		}

		if(cmd == 1){
			cmd = CLOSE_CMD;
		}
		else if(cmd == 2){
			cmd = OPEN_CMD;
		}
		else if(cmd == 3){
			cmd = SETPERIOD_CMD;
			printf("input timer period:");
			ret = scanf("%d", &arg);
			if(ret!=1){
				gets(str);
			}
		}
		ioctl(fd, cmd, arg);
	}

	ret = close(fd);  // close文件
	if(ret <0){
		printf("file %s close failed\r\n",argv[1]);
		return -1;
	}

	return 0;
}
